package com.yzl.springboot.springboot.controller;

import com.yzl.springboot.springboot.domain.User;
import com.yzl.springboot.springboot.service.UserService;
import com.yzl.springboot.springboot.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Title: UserController
 * @Author: 姚志立
 * @Description:
 * @Date: 2020/5/24 10:56
 * @Version: 1.0
 */

@RestController
@RequestMapping("api/v1/pub/user")
public class UserController {
//    @PostMapping("login")
//    public JsonData login(String username,String pwd){
//        System.out.println("username:"+username+" password:"+pwd);
//        return JsonData.buildSuccess("");
//    }
//}
    @Autowired
    public UserService userService;

    /**
     * 登陆接口
     * @param user
     * @return
     */
    @PostMapping("login")
    public JsonData login(@RequestBody User user){
        System.out.println("user="+user.toString());
        String token=userService.login(user.getUsername(), user.getPwd());
        return token==null?JsonData.buildError("账号密码错误"):JsonData.buildSuccess(token);
    }

    /**
     * 列出全部用户
     * @return
     */
    @GetMapping("list")
    public JsonData listUser(){
        return JsonData.buildSuccess(userService.listUser());
    }
}
